package ReflectionGetField;


public class Main {
    public static void main(String[] args) throws Exception {
        // 用 Class 类对象 stdClass 通过反射获取到 Student 类的类结构信息
        Class stdClass = Student.class;
        // 获取public字段"score":
        System.out.println(stdClass.getField("score"));
        // 获取继承的public字段"name":
        System.out.println(stdClass.getField("name"));
        // 获取private字段"grade":
        System.out.println(stdClass.getDeclaredField("grade"));

        System.out.println();
        // 获取public方法getScore，参数为String:
        System.out.println(stdClass.getMethod("getScore", String.class));
        // 获取继承的public方法getName，无参数:
        System.out.println(stdClass.getMethod("getName"));
        // 获取private方法getGrade，参数为int:
        System.out.println(stdClass.getDeclaredMethod("getGrade", int.class));
    }
}

// 学生类，继承自 Person
// 扩展了两个字段：score , grade
class Student extends Person {
    public int score;
    private int grade;

    public int getScore(String type) {
        return 99;
    }
    private int getGrade(int year) {
        return 1;
    }
}

// 定义人类，父类，只有一个 name 字段
class Person {
    public String name;
    public String getName() {
        return "Person";
    }
}

